package metar_taf;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

public class Metar {
	public String ICAO = "";
	public Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
	public List<String> wind = new ArrayList<String>();
	public String visibility = "";
	public List<String> weather = new ArrayList<String>();
	public List<String> clouds = new ArrayList<String>();
	public String temperature = "";
	public String dewpoint = "";
	public String pressure = "";
	public String metar = "";

	private final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm");

	public String toJSON(TimeZone timeZone) {
		StringBuilder sb = new StringBuilder();
		sb.append("{\n");
		{
			sb.append("\"airport\": \"" + ICAO + "\",\n");
			dateFormat.setTimeZone(timeZone);
			sb.append("\"date\" : \"" + dateFormat.format(date.getTime())
					+ "\",\n");
			sb.append("\"wind\" : ");
			listToJSON(sb, wind);
			sb.append(",\n");
			sb.append("\"visibility\" : \"" + visibility + "\",\n");
			sb.append("\"weather\" : ");
			listToJSON(sb, weather);
			sb.append(",\n");
			sb.append("\"clouds\" : ");
			listToJSON(sb, clouds);
			sb.append(",\n");
			sb.append("\"temperature\" : \"" + temperature + "\",\n");
			sb.append("\"dewpoint\" : \"" + dewpoint + "\",\n");
			sb.append("\"pressure\" : \"" + pressure + "\",\n");
			sb.append("\"metar\" : \"" + metar + "\"\n");
		}
		sb.append("}");
		return sb.toString();
	}

	private void listToJSON(StringBuilder sb, List<String> list) {
		sb.append("[");
		for (Iterator<String> it = list.iterator(); it.hasNext();) {
			sb.append("\"" + it.next() + "\"");
			if (it.hasNext()) {
				sb.append(',');
			}
		}
		sb.append("]");
	}
}
